package com.babyshop.web;

import java.util.ArrayList;
import java.util.List;

import com.babyshop.dao.TypeDao;
import com.babyshop.dao.TypeDaoImpl;
import com.babyshop.domain.Type;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

public class TypeAction extends ActionSupport implements ModelDriven<Type> {
	
	private static final long serialVersionUID = -6659925652584240539L;
	private Type type = new Type();
	private List<Type> listTypes = new ArrayList<Type>();
	private TypeDao typeDao = new TypeDaoImpl();
	
	public Type getType() {
		return type;
	}

	public void setType(Type type) {
		this.type = type;
	}

	public List<Type> getListTypes() {
		return listTypes;
	}

	public void setListTypes(List<Type> listTypes) {
		this.listTypes = listTypes;
	}

	@Override
	public Type getModel() {
		return type;
	}
	
	public String saveOrUpdate(){
		typeDao.saveOfUpdate(type);
		return SUCCESS;
	}
	
	public String delete(){
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		typeDao.delete(Integer.parseInt(request.getParameter("id")));
		return SUCCESS;	
	}
	
	public String edit(){
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		type = typeDao.get(Integer.parseInt(request.getParameter("id")));
		return SUCCESS;	
	}
	
	public String getAll(){
		listTypes = typeDao.getAll();
		return SUCCESS;
	}
	
	public String get(){
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		type = typeDao.get(Integer.parseInt(request.getParameter("id")));
		return SUCCESS;
	}

}
